package com.fengba.nanzhi.model;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.j256.ormlite.stmt.Where;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by duan on 2018/4/17.
 * 完成对数据库的CRUD的操作
 */

public class UserDao {
    private Context context;
    private Dao<User,Integer> userDao;
    private DataBaseHelper helper;

    public UserDao(Context context){
        this.context = context;
        helper = DataBaseHelper.getInstance(context);
        try {
            userDao = helper.getDao(User.class);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public  void addUser(User user){
        try {
            userDao.create(user);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public  void updateUser(User user){
        try {
            userDao.update(user);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void updateUserByBuilder(User user){
        try{
            UpdateBuilder builder = userDao.updateBuilder();
            //builder.updateColumnValue("name")
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void deleteUser(User user){
        try {
            userDao.delete(user);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void deleteMulUser(List<User> users){
        try {
            userDao.delete(users);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void deleteUserByID(int id){
        try {
             userDao.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * select
     * @return
     */
    public List<User> listAll(){
        try {
            return userDao.queryForAll();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public User queryUserByID(int id){
        User user= null;
        try {
            user = userDao.queryForId(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 查询条件1
     * select * form user_info where age>23
     * @return
     */
    public List<User> queryBuilider1(int user_id){
        List<User> list = null;
        QueryBuilder<User,Integer> queryBuilder = userDao.queryBuilder();
        Where<User,Integer> where = queryBuilder.where();
        try {
            where.eq("user_id",user_id);
            where.prepare();
            //select * form user_info where name="端端" and desc="帅哥"
            list = queryBuilder.query();
            //list = queryBuilder.where().eq("name","张宇").and().eq("desc","歌手").query();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }

    public List<User> queryBuilder2(){
        List<User> list = null;
        QueryBuilder<User,Integer> queryBuilder = userDao.queryBuilder();
        Where<User,Integer> where = queryBuilder.where();
//        try {
//            where.or(where.and(where.eq("",""),where.eq("","")),where.and())
//        }catch (SQLException e){
//            e.printStackTrace();
//        }
        return list;
    }
}
